package cn.jm.xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlStudy {

	public void write() {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		try {
			Document document = DocumentHelper.createDocument();
			Element root = document.addElement("root");
			root.setAttributeValue("id", "jingmin");

			XMLWriter writer = new XMLWriter(new FileWriter("output.xml"), format);
			writer.write(document);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public List<Map<String, String>> read() {
		SAXReader reader = new SAXReader();
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		try {
			Document document = reader.read(new File("output.xml"));
			Element root = document.getRootElement();
			Iterator<Element> iterator = root.elementIterator();
			while (iterator.hasNext()) {
				Element e = iterator.next();
				Map<String, String> item = new HashMap<String, String>();
				item.put("name", e.elementText("name"));
				item.put("sex", e.elementText("sex"));
				data.add(item);
			}

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}

	public List<Map<String, String>> builderData() {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		Map<String, String> item = new HashMap<String, String>();
		item.put("name", "张三");
		item.put("sex", "男");
		data.add(item);
		item = new HashMap<String, String>();
		item.put("name", "李四");
		item.put("sex", "女");
		data.add(item);
		item = new HashMap<String, String>();
		item.put("name", "王四");
		item.put("sex", "男");
		data.add(item);
		item = new HashMap<String, String>();
		item.put("name", "邵明");
		item.put("sex", "女");
		data.add(item);
		for(int i=0 ; i<100;i++) {
			item = new HashMap<String, String>();
			item.put("name", "邵明"+i);
			item.put("sex", "女");
			data.add(item);
		}

		return data;
	}

	public void writeData() {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		try {
			Document document = DocumentHelper.createDocument();
			Element root = document.addElement("root");
			List<Map<String, String>> data = builderData();
			for (Map<String, String> item : data) {
				Element people = root.addElement("people");
				Element nameElement = people.addElement("name");
				Element sexElement = people.addElement("sex");
				nameElement.setText(item.get("name"));
				sexElement.setText(item.get("sex"));
			}

			XMLWriter writer = new XMLWriter(new FileWriter("output.xml"), format);
			writer.write(document);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
